//
//  YHKPhotoView.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/3/7.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit

//标识
let ReuseIdentifier = "ReuseIdentifier"

let photoViewMargin:CGFloat = 10
let photoItmeMargin:CGFloat = 5
class YHKPhotoView: UICollectionView, UICollectionViewDataSource,UICollectionViewDelegate,YHKPhotoCollectionViewCellDelgate{
    
    //定义一个闭包
    var closure: (() -> ())?
    
   //定义一个私有的数组，用来存放collectionView的图片
    private var imageArray:[UIImage] = [UIImage]()

  //流水布局
    let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setUpView()
        

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置属性抽出的方法
    private func setUpView(){
        
        backgroundColor = UIColor.orangeColor()
        
        //设置代理
        dataSource = self
        delegate = self
        
        //注册cell
        registerClass(YHKPhotoCollectionViewCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        
        //设置item的属性
        
        //屏幕的宽 - 间距 * 2 - itme间距 * 2
      let itemHW = (kUIScreenWidth - photoViewMargin * 2 - photoItmeMargin * 3)/3
        flowLayout.itemSize = CGSizeMake(itemHW, itemHW)
        flowLayout.minimumInteritemSpacing = photoItmeMargin
        flowLayout.minimumLineSpacing = photoItmeMargin
        
    }
    
    //MARK：--对外公开一个方法，添加图片
    func addImages(image: UIImage){
        
        //添加
        imageArray.append(image)
        
        //每次拿到数据之后，都要更新shuju 
        reloadData()
        
    }
    

}






//MARK:--CollectionView的数据源方法
extension YHKPhotoView{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
         if imageArray.count == 0 || imageArray.count == 9{
            
            return imageArray.count
            
         }else{
            return imageArray.count + 1
        }
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! YHKPhotoCollectionViewCell
        
        //设置代理
        cell.delgate = self
        
        
        //数据传递===数组越界
        if indexPath.item >= imageArray.count{
            
            cell.iconImage = nil
            
        }else{
            
         cell.iconImage = imageArray[indexPath.item]
            
        }
         cell.myIndexPath = indexPath
        
    
        return cell
    }
    

    //代理方法的实现
    func deletePicture(index: Int) {
        //通过下标来删除图片
        imageArray.removeAtIndex(index)
        
        reloadData()
        
    }
    
    
    //cell的点击事件调用的方法
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //点击最后一个item时候才能执行下面的方法
        if indexPath.item == imageArray.count{
            
            print("选择图片")
            
            //通过调用闭包，来实现控制器调用自己的方法
            closure?()
        }

    }
    

    
    
    
    
}






